package com.fujimana.android.manacode.utils;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.util.zip.ZipFile;

public class Utils {

	public static void close(ZipFile zf) {
		if (zf == null) {
			return;
		}

		try {
			zf.close();
		} catch (IOException e) {
			// nothing to do.
		}
	}

	public static void close(Closeable closeable) {
		if (closeable == null) {
			return;
		}

		try {
			closeable.close();
		} catch (IOException e) {
			// nothing to do.
		}
	}

	public static String padding(int paddingNum, int num, char padding) {
		StringBuilder sb = new StringBuilder();

		String str = "" + num;
		int count = paddingNum - str.length();

		for (int i = 0; i < count; i++) {
			sb.append(padding);
		}
		sb.append(str);

		return sb.toString();
	}

	public static String reader2String(Reader reader) throws IOException {
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();

		try {
			br = new BufferedReader(reader);

			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}

		} finally {
			Utils.close(reader);
			Utils.close(br);
		}

		return sb.toString();

	}

	public static final String escapeHTML(String s) {
		StringBuffer sb = new StringBuffer();
		int n = s.length();
		for (int i = 0; i < n; i++) {
			char c = s.charAt(i);
			switch (c) {
			case '<':
				sb.append("&lt;");
				break;
			case '>':
				sb.append("&gt;");
				break;
			case '&':
				sb.append("&amp;");
				break;
			case '"':
				sb.append("&quot;");
				break;

			default:
				sb.append(c);
				break;
			}
		}
		return sb.toString();
	}
}
